import Taro from '@tarojs/taro'
import {isWeChat, jumpUrl} from '@/utils/utils'
import {GOODS_STRATEGY_TYPE, GOODS_TYPE} from '@/config/types'
import { setShopCartGlobal, verifyUserLogin } from '@/utils/storage'
import {AUTH_TOKEN} from "@/config/storage";
import { getAppConfig } from '@/utils/storage'

/**
 * 跳转直播间
 */
// export function toLivePlayerPlugin() {
//   let roomId = ['1'] // 填写具体的房间号，可通过下面【获取直播房间列表】 API 获取
//   let customParams = encodeURIComponent(JSON.stringify({ path: 'pages/index/index', pid: 1 })) // 开发者在直播间页面路径上携带自定义参数（如示例中的path和pid参数），后续可以在分享卡片链接和跳转至商详页时获取，详见【获取自定义参数】、【直播间到商详页面携带参数】章节（上限600个字符，超过部分会被截断）
//   Taro.navigateTo({
//     url: `plugin-private://${process.env.APP_ID}/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
//   })
// }

/**
 * 跳转任意界面
 */
export function toAnyPage(url, data?, isRedirect?) {
  jumpUrl(url, data, isRedirect)
}


/**
 * 跳转任意界面 (tabbar)
 */
export function toAnyPageNew (url) {
  const ind = [
    '/pages/tabbar/home/index',
    '/pages/tabbar/cart/index',
    '/pages/tabbar/user/index',
    '/pages/tabbar/category/index',
    '/pages/tabbar/comment/index',
    '/pages/tabbar/popular/index',
    '/pages/bindingmobile/index'
  ].findIndex(path => path === url)
  if (ind >= 0) {
    Taro.switchTab({url})
  } else {
    Taro.navigateTo({url})
  }
  // Taro[ind >= 0 ? 'switchTab' : 'navigateTo']({ url: url })
}

/**
 * 跳转到首页
 */
export function toHomePage() {
  // Taro.switchTab({url: '/pages/tabbar/home/index'})
  window.location.href = '/pages/tabbar/home/index'
  // Taro.navigateTo({url: '/pages/tabbar/home/index'})
}

/**
 * 客服or投诉
 */
export function toServiceComplaint() {
  Taro.navigateTo({url: '/pages/service/index'})
}

/**
 * 客服
 */
export function toService() {
  window.location.href = getAppConfig('seriveLink')
}

/**
 * 投诉
 */
export function toComplaint() {
  Taro.navigateTo({url: '/pages/service/complaint-index'})
}


/* ------------------------------------------------------- 商品分类 ------------------------------------------------------- */
/* 分类页面 */
export function toCategoryPage() {
  Taro.switchTab({url: '/pages/tabbar/category/index'})
}

/* 搜索 */
export function toSearchPage(params?, isRedirect?) {
  jumpUrl('/pages/search/index', params, isRedirect)
}
/* ------------------------------------------------------- 商品分类 ------------------------------------------------------- */




/* ------------------------------------------------------- 购物车 ------------------------------------------------------- */
/* 购物车 */
export function toCartPage() {
  Taro.switchTab({url: '/pages/tabbar/cart/index'})
}
/* ------------------------------------------------------- 购物车 ------------------------------------------------------- */



/* ------------------------------------------------------- 评价功能 ------------------------------------------------------- */
/* 口碑好评 */
export function toCommentPage() {
  Taro.switchTab({url: '/pages/tabbar/comment/index'})
}

/* 订单评论列表 */
export function toCommentListPage(id, isRedirect?) {
  jumpUrl('/pages/comment/list/index', {id}, isRedirect)
}

/* 评论详情 */
export function toCommentInfoPage(id, isRedirect?) {
  jumpUrl('/pages/comment/detail/index', {id}, isRedirect)
}

/* 商品评价 */
export function toCommentGoodsPage(id, isRedirect?) {
  jumpUrl('/pages/comment/goods/index', {id}, isRedirect)
}
/* ------------------------------------------------------- 评价功能 ------------------------------------------------------- */




/* ------------------------------------------------------- 用户界面 ------------------------------------------------------- */
/* 用户中心 */
export function toUserPage() {
  Taro.switchTab({url: '/pages/tabbar/user/index'})
}

/* 用户登录 */
export function toUserLoginPage(isRedirect?) {
  // const token = Taro.getStorageSync(AUTH_TOKEN)
  // const auth = Taro.getStorageSync('AUTH_ORIGINAL')
  // if (isWeChat() && token && !auth) {
  //   console.log('----------------------')
  //   return
  // }
  jumpUrl('/pages/login/index', {}, isRedirect)
}

/* 我的团队 */
export function toUserTeamPage(type?, isRedirect?) {
  jumpUrl('/pages/user/team/index', { type }, isRedirect)
}
/* ------------------------------------------------------- 用户界面 ------------------------------------------------------- */



/* ------------------------------------------------------- 店铺 ------------------------------------------------------- */
// /* 店铺列表 */
// export function toShopPage() {
//   Taro.switchTab({url: '/pages/tabbar/shop/index'})
// }

/* 店铺信息 */
export function toShopInfoPage(id, isRedirect?) {
  jumpUrl('/pages/shop/info/index', {id}, isRedirect)
}

/* 店铺评论 */
export function toShopCommentPage(id, isRedirect?) {
  jumpUrl('/pages/shop/comment/index', {id}, isRedirect)
}
/* ------------------------------------------------------- 店铺 ------------------------------------------------------- */



/* ------------------------------------------------------- 订单界面 ------------------------------------------------------- */
/* 计算订单 */
export function toCalcOrderPage(params:any, isRedirect?) {
  verifyUserLogin({
    success: function () {
      // setShopCartGlobal(cartList)
      jumpUrl('/pages/order/calc/index', params, isRedirect)
    },
    fail: function () {
      toUserLoginPage()
    }
  })
}

/* 计算订单 */
export function toCalcVirtualOrder(params:any, isRedirect?) {
  verifyUserLogin({
    success: function () {
      jumpUrl('/pages/virtual/orderCalc/index', params, isRedirect)
    },
    fail: function () {
      toUserLoginPage()
    }
  })
}

// /* 添加实物商品订单 */
// export function toAddRealOrderPage(cartList, isRedirect?) {
//   setShopCartGlobal(cartList)
//
//   verifyUserLogin({
//     success: function () {
//       jumpUrl('/pages/order/add/real/index', {}, isRedirect)
//     },
//     fail: function () {
//       toUserLoginPage()
//     }
//   })
// }

/* 订单列表 */
export function toOrderListPage(isRedirect?) {
  jumpUrl('/pages/order/list/index', {}, isRedirect)
}

/* 订单支付页面 */
export function toOrderUnpaidPage(id, isRedirect?) {
  jumpUrl(`/pages/order/unpaid/index`, {id}, isRedirect)
}

/* 订单信息 */
export function toOrderInfoPage(id, isRedirect?) {
  jumpUrl('/pages/order/info/index', {id}, isRedirect)
}

/* 订单核销码 */
export function toOrderVerificationPage(id, isRedirect?) {
  jumpUrl('/pages/order/verification/index', {id}, isRedirect)
}

/* 订单评价 */
export function toOrderCommentPage(id, isRedirect?) {
  jumpUrl('/pages/order/comment/index', {id}, isRedirect)
}
/* ------------------------------------------------------- 订单界面 ------------------------------------------------------- */





/* ------------------------------------------------------- 商品详情 ------------------------------------------------------- */
/* 商品详情 */
export function toGoodsPage({ id, type, saleStrategy }:any, isRedirect?) {
  const newType = saleStrategy === GOODS_STRATEGY_TYPE.NORMAL ? type : saleStrategy
  switch(newType) {
    case GOODS_TYPE.SECKILL:
      jumpUrl('/pages/real/info/index', {id}, isRedirect)
      break
    case GOODS_TYPE.PINK:
      jumpUrl('/market/pages/group/goods/index', {id}, isRedirect)
      break
    case GOODS_TYPE.SCORE:
      jumpUrl('/market/pages/integral/goods/index', {id}, isRedirect)
      break
    case GOODS_TYPE.VIRTUAL:
      jumpUrl('/pages/virtual/info/index', {id}, isRedirect)
      break
    case GOODS_TYPE.REAL:
      jumpUrl('/pages/real/info/index', {id}, isRedirect)
      break
    default:
      jumpUrl('/pages/real/info/index', {id}, isRedirect)
  }
}
/* ------------------------------------------------------- 商品详情 ------------------------------------------------------- */





/* ------------------------------------------------------- 会员页面 ------------------------------------------------------- */
/* 开启会员 */
export function toMemberOpenPage(isRedirect?) {
  jumpUrl('/pages/member/open/index', {}, isRedirect)
}

/* 会员支付 */
export function toMemberPayPage(isRedirect?) {
  jumpUrl('/pages/member/pay/index', {}, isRedirect)
}

/* 会员收藏列表 */
export function toMemberFavoritePage(isRedirect) {
  jumpUrl('/pages/member/favorite/index', {}, isRedirect)
}

/* 用户地址页面 */
export function toAddressListPage(isRedirect?) {
  jumpUrl('/pages/address/list/index', {}, isRedirect)
}

/* 用户地址详情 */
export function toAddressInfoPage(id, isRedirect?) {
  jumpUrl('/pages/address/info/index', {id}, isRedirect)
}
/* ------------------------------------------------------- 会员页面 ------------------------------------------------------- */




/* ------------------------------------------------------- 店铺页面 ------------------------------------------------------- */
/* 店铺主页 */
export function toStoreHomePage(isRedirect) {
  jumpUrl('/pages/store/home/index', {}, isRedirect)
}
/* ------------------------------------------------------- 店铺页面 ------------------------------------------------------- */





/* ------------------------------------------------------- 提现 ------------------------------------------------------- */

/* 我的推广 */
export function toCashHomePage(isRedirect?) {
  jumpUrl('/pages/cash/home/index', {}, isRedirect)
}

/* 提现记录 */
export function toCashRecordPage(isRedirect?) {
  jumpUrl('/pages/cash/record/index', {}, isRedirect)
}

/* 申请提现 */
export function toCashApplyPage(isRedirect?) {
  jumpUrl('/pages/cash/apply/index', {}, isRedirect)
}

/* 佣金列表 */
export function toCommissionRecordPage(isRedirect?) {
  jumpUrl('/pages/commission/record/index', {}, isRedirect)
}

/* ------------------------------------------------------- 提现 ------------------------------------------------------- */



/* ------------------------------------------------------- 新闻资讯 ------------------------------------------------------- */

/* 新闻列表 */
export function toNewsListPage() {
  jumpUrl('/pages/news/list/index')
}

/* 新闻详情 */
export function toNewsInfoPage(id, isRedirect?) {
  jumpUrl('/pages/news/info/index', {id}, isRedirect)
}

/* ------------------------------------------------------- 新闻资讯 ------------------------------------------------------- */



/* ------------------------------------------------------- 系统页面 ------------------------------------------------------- */
/* 关于我们 */
export function toAboutPage(isRedirect) {
  jumpUrl('/pages/about/index', {}, isRedirect)
}

/* 设置页面 */
export function toSettingPage(isRedirect) {
  jumpUrl('/pages/setting/index', {}, isRedirect)
}

/* 打开网页 */
export function toWebPage(url, isRedirect?) {
  jumpUrl('/pages/web/index', {url}, isRedirect)
}
/* ------------------------------------------------------- 系统页面 ------------------------------------------------------- */






/* ------------------------------------------------------- 优惠券 ------------------------------------------------------- */
/* 优惠券领取 */
export function toCouponReceivePage(isRedirect?) {
  jumpUrl('/market/pages/coupon/receive/index', {}, isRedirect)
}

/* 我的优惠券 */
export function toCouponPage(isRedirect?) {
  jumpUrl('/market/pages/coupon/home/index', {}, isRedirect)
}
/* ------------------------------------------------------- 优惠券 ------------------------------------------------------- */



/* ------------------------------------------------------- 用户积分（积分商城） ------------------------------------------------------- */
/* 用户积分 */
export function toIntegralPage(isRedirect?) {
  jumpUrl('/market/pages/integral/home/index', {}, isRedirect)
}

/* 积分签到 */
export function toIntegralCheckinPage(isRedirect?) {
  jumpUrl('/market/pages/integral/checkin/index', {}, isRedirect)
}

/* 积分记录 */
export function toIntegralRecordPage(params?, isRedirect?) {
  jumpUrl('/market/pages/integral/record/index', params, isRedirect)
}

/* 积分商品详情 */
export function toIntegralGoodsInfoPage(id, isRedirect?) {
  jumpUrl('/market/pages/integral/goods/index', {id}, isRedirect)
}

/* 添加积分商品订单 */
export function toAddIntegralOrderPage(params, isRedirect?) {
  jumpUrl('/market/pages/integral/order/add/index', params, isRedirect)
}

/* 订单列表 */
export function toIntegralOrderListPage(isRedirect?) {
  jumpUrl('/market/pages/integral/order/list/index', {}, isRedirect)
}

/* 订单详情 */
export function toIntegralOrderInfoPage(id, isRedirect?) {
  jumpUrl('/market/pages/integral/order/info/index', {id}, isRedirect)
}

/* ------------------------------------------------------- 用户积分（积分商城） ------------------------------------------------------- */



/* ------------------------------------------------------- 用户钱包 ------------------------------------------------------- */
/* 用户钱包 */
export function toWalletPage(isRedirect?) {
  jumpUrl('/market/pages/wallet/home/index', {}, isRedirect)
}

/* 钱包充值 */
export function toWalletRechargePage(isRedirect?) {
  jumpUrl('/market/pages/wallet/recharge/index', {}, isRedirect)
}

/* 钱包记录 */
export function toWalletRecordPage(type?, isRedirect?) {
  jumpUrl('/market/pages/wallet/record/index', {type}, isRedirect)
}
/* ------------------------------------------------------- 用户钱包 ------------------------------------------------------- */



/* ------------------------------------------------------- 秒杀 ------------------------------------------------------- */
/* 秒杀列表 */
export function toSeckillListPage(isRedirect?) {
  jumpUrl('/market/pages/seckill/list/index', {}, isRedirect)
}
/* ------------------------------------------------------- 秒杀 ------------------------------------------------------- */



/* ------------------------------------------------------- 拼团 ------------------------------------------------------- */
/* 待拼团 */
export function toGroupListPage(isRedirect?) {
  jumpUrl('/market/pages/group/list/index', {}, isRedirect)
}
/* 待拼团 */
export function toWaitGroupPage(id, isRedirect?) {
  jumpUrl('/market/pages/group/wait/index', {id}, isRedirect)
}

/* ------------------------------------------------------- 拼团 ------------------------------------------------------- */











/* ------------------------------------------------------- 直播 ------------------------------------------------------- */
/* 直播列表 */
export function toLiveListPage(isRedirect?) {
  jumpUrl('/live/pages/list/index', {}, isRedirect)
}

/* 直播详情 */
export function toLiveInfoPage(room_id) {
  // jumpUrl('/live/pages/detail/index', {id})
  jumpUrl('plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin', {room_id})
}

/* ------------------------------------------------------- 直播 ------------------------------------------------------- */
/* ------------------------------------------------------- 绑定手机 ------------------------------------------------------- */
/* 绑定手机 */
export function tobindingmobile(isRedirect?) {
  jumpUrl('/pages/bindingmobile/index', {}, isRedirect)
}



/* ------------------------------------------------------- 直播 ------------------------------------------------------- */
